package com.huosu.utils;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huosu.commons.enums.CheckPoolSubErrorCode;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @program: miner-shield-web-v2
 * @description:
 * @author: changlin
 * @create: 2020-02-10 20:49
 **/
public class PoolAddressUtil {

//    public static CheckPoolSubErrorCode checkPoolAndSubAccount(String coin, String poolAddressAndPort, String subAccount) {
//
//        if (StringUtil.isEmpty(coin)) {
//            return CheckPoolSubErrorCode.SUCCESS;
//        }
//
//        String connect = "{\"id\": 1, \"agent\":\"mingshieldminer\",\"method\": \"mining.subscribe\", \"params\": [\"mingshield-miner/0.0.9\"]}" + "\r\n";
//        String eth_connect = "{\"id\": 1,\"jsonrpc\": \"2.0\",\"agent\":\"mingshieldminer\", \"method\": \"eth_submitLogin\", \"worker\": \"\" , \"params\": [\"" + subAccount + "\", \"x\"]}" + "\r\n";
//        String login = "{\"id\": 2,\"agent\":\"mingshieldminer\", \"method\": \"mining.authorize\", \"params\": [\"" + subAccount + "\", \"1\"]}" + "\r\n";
//
//        try {
//            String address = "", port = "";
//
//            if (StringUtil.isNotEmpty(poolAddressAndPort)) {
//                String[] addressAndPort = poolAddressAndPort.split(":");
//                if (addressAndPort.length == 2) {
//                    address = addressAndPort[0];
//                    port = addressAndPort[1];
//                } else {
//                    return CheckPoolSubErrorCode.POOL_ERROR;
//                }
//            } else {
//                return CheckPoolSubErrorCode.POOL_ERROR;
//            }
//
//            Socket sock = new Socket(address, NumberUtil.parseInt(port));
//            OutputStream os = sock.getOutputStream();
//
//            if ("eth".equals(coin)) {
//                os.write(eth_connect.getBytes());
//            }
//
//            if ("btc".equals(coin)) {
//                os.write(connect.getBytes());
//                os.write(login.getBytes());
//            }
//
//            BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
//            String message = "";
//
//            message = in.readLine();
//            if ("btc".equals(coin)) {
//                if (message != null) {
//                    JSONObject jsonObject = JSON.parseObject(message);
//                    if (!StringUtil.isEmpty(jsonObject.getString("error"))) {
//                        return CheckPoolSubErrorCode.POOL_RETURN_ERROR;
//                    }
//                } else {
//                    return CheckPoolSubErrorCode.POOL_RETURN_ERROR;
//                }
//            }
//
//            message = in.readLine();
//            if (message != null) {
//                JSONObject jsonObject = JSON.parseObject(message);
//                if (!StringUtil.isEmpty(jsonObject.getString("error"))) {
//                    return CheckPoolSubErrorCode.LOGIN_FAIL;
//                }
//            } else {
//                return CheckPoolSubErrorCode.LOGIN_FAIL;
//            }
//
//        } catch (Exception e) {
//            return CheckPoolSubErrorCode.FAIL;
//        } finally {
//
//        }
//
//        return CheckPoolSubErrorCode.SUCCESS;
//    }

}
